﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using Maticsoft.DBUtility;

namespace QunYing.DAL
{
     public partial class UserLoginInfo
    {
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="strUserName">用户ID</param>
        /// <param name="strUserPwd">密码</param>
        /// <returns></returns>
        public ArrayList Login(string strUserName, string strUserPwd)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"select top 1  a.用户ID,b.经销商编号,isNull(b.用户姓名,c.名称) AS 用户姓名,a.角色,isNULL(b.到期时间,c.资格截止日期) AS 到期时间
                            from dbo.UserLoginInfo a left join dbo.VipInfo b on a.用户ID=b.用户ID
                            left join dbo.DealerInfo c on a.用户ID=c.编号");
            strSql.Append(" where (a.用户ID=@ID or b.移动电话=@ID or b.电邮地址=@ID or b.会员卡号=@ID) and a.密码=@密码");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.NVarChar,20),
                    new SqlParameter("@密码",SqlDbType.NChar,32)
                                        };
            parameters[0].Value = strUserName;
            parameters[1].Value = strUserPwd;
            DataTable dt = DbHelperSQL.Query(strSql.ToString(), parameters).Tables[0];
            ArrayList al = new ArrayList();
            if (dt.Rows.Count > 0)
            {
                al.Add(dt.Rows[0]["用户ID"].ToString());
                al.Add(dt.Rows[0]["用户姓名"].ToString());
                al.Add(dt.Rows[0]["角色"].ToString());
                al.Add(dt.Rows[0]["到期时间"].ToString());
                al.Add(dt.Rows[0]["经销商编号"].ToString());
            }
            return al;
        }

        /// <summary>
        /// 更新登录时间
        /// <param name="strUserName">用户ID</param>
        /// </summary>
        public bool UpdateTime(string strUserName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update UserLoginInfo set ");
            strSql.Append("最后登录时间=getdate()");
            strSql.Append(" where 用户ID=@用户ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@用户ID", SqlDbType.NVarChar,20)
                                        };
            parameters[0].Value = strUserName;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 修改密码
        /// <param name="strUserName">用户ID</param>
        /// <param name="strNewPwd">密码</param>
        /// </summary>
        public bool UpdatePwd(string strUserName, string strNewPwd)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update UserLoginInfo set ");
            strSql.Append("密码=@密码");
            strSql.Append(" where 用户ID=@用户ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@用户ID", SqlDbType.NVarChar,20),
                    new SqlParameter("@密码", SqlDbType.NVarChar,20)
                                        };
            parameters[0].Value = strUserName;
            parameters[1].Value = strNewPwd;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
